<template>
	<view>
	    <view>
	        <view class='header'>
	            <image src='/static/images/wechat-circle.png'></image>
	        </view>
	 
	        <view class='content'>
	            <view>申请获取以下权限</view>
	            <text>获得你的公开信息</text>
				 <!-- <text>获得你的公开信息(昵称，头像等)</text> -->
	        </view>
			<view class="btnBox">
				<!-- <u-button open-type="getUserInfo">获取授权</u-button> -->
				<u-button type="primary" shape="square"  class="btn" @click="login()">授权登录</u-button>
			</view>
	    </view>
	</view>

</template>

<script>
	import {doLogin} from "@/api/api"
	export default {
		data() {
			return {
			
			}
		},
		
		methods:{
			login(){
				let code = ''
				console.log('do_login')
				uni.login({
					success:(res)=>{
						code = res.code
						
					},
					fail:(res)=>{
						console.log(res)
						console.log('rejuct')
					}
				})
				
				uni.getUserProfile({
					desc: '用于完善会员资料',
					success:(res2)=>{
						console.log('res2')
						console.log(res2)
						if(res2.errMsg=='getUserProfile:ok'){
							var data = {
								code:code,
								gender:res2.userInfo.gender,
								country:res2.userInfo.country,
								province:res2.userInfo.province,
								city:res2.userInfo.city,
							}
							doLogin(data).then(res3=>{
								if(res3.code==0){
									
									//登录成功，保存token
									uni.setStorageSync('token',res3.data.token)
									uni.setStorageSync('userinfo',res3.data.userinfo)
									uni.setStorageSync('user_id',res3.data.userinfo.id)
									uni.setStorageSync('is_login',true)
									uni.showToast({
										title: res3.msg,
										icon: 'none',
									});
									uni.setStorageSync('login_back',true)
									setTimeout(function(){
										 uni.navigateBack({//返回
											   delta: 1
											 })
									},1500)
								}else{
									//登录失败
									uni.showToast({
										title: res3.msg,
										icon: 'none',
									});
								}
								
							})
						}else{
							uni.showToast({
								title: '获取用户资料失败，请联系管理员',
								icon: 'none',
							});
						}
						
					},
					fail:(res2)=>{
						uni.showToast({
							title: '登录失败，您拒绝了授权',
							icon: 'none',
						});
					}
				})
				
			}
		
		}
	}
</script>
<style>
	page{background-color: #F5F5F5;}
</style>
<style scoped>
	.header {
	  margin: 90rpx 0 90rpx 50rpx;
	  border-bottom: 1px solid #ccc;
	  text-align: center;
	  width: 650rpx;
	  height: 300rpx;
	  line-height: 450rpx;
	}
	
	.header image {
	  width: 200rpx;
	  height: 200rpx;
	}
	
	.content {
	  margin-left: 50rpx;
	  margin-bottom: 90rpx;
	}
	
	.content text {
	  display: block;
	  color: #9d9d9d;
	  margin-top: 40rpx;
	}
	
	.btn{width: 600rpx;height: 80rpx;background: #0066ff;margin-top: 50rpx;
	line-height: 80rpx;text-align: center;border-radius: 100rpx;
	color: #ffffff;font-size: 35rpx;font-weight: bold;letter-spacing: 10rpx;
	}
	.btnBox{display: flex;justify-content: center;width: 100%;}
</style>